<template>
    <div class="step1">
        <el-form
            ref="formRef"
            class="step_form"
            :model="propsFormData"
            :rules="formRule"
            label-width="120px"
            :disabled="props.look">
            <div class="con_title">规则基本信息</div>
            <el-form-item label="规则名称" prop="name">
                <el-input v-model="propsFormData.name" maxlength="15" placeholder="请输入规则名称" show-word-limit />
            </el-form-item>
            <el-form-item
                :class="{ mar0_form_item: propsFormData.validity_type == 2 }"
                label="规则有效期"
                prop="validity_type">
                <el-radio-group v-model="propsFormData.validity_type">
                    <el-radio label="1" value="1">永久</el-radio>
                    <el-radio label="2" value="2">固定期限</el-radio>
                </el-radio-group>
            </el-form-item>
            <template v-if="propsFormData.validity_type == 2">
                <el-form-item class="notShow_formItem_required" label=" " prop="validity_time">
                    <el-date-picker
                        v-model="propsFormData.validity_time"
                        type="datetimerange"
                        format="YYYY-MM-DD HH:mm:ss"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        :default-time="defaultTime()"
                        :disabled-date="disabledDate" />
                </el-form-item>
            </template>
            <el-form-item label="规则描述">
                <el-input
                    v-model="propsFormData.desc"
                    :autosize="{
                        minRows: 4
                    }"
                    placeholder="请输入规则描述"
                    type="textarea"
                    maxlength="50"
                    show-word-limit />
            </el-form-item>
            <div class="con_title step1_two">SOP触发范围</div>
            <el-form-item label="选择群主" class="" prop="groupchat_ids">
                <div>
                    <el-button type="primary" :disabled="creators.length >= 10" @click="openGroup">选择员工</el-button>
                    <div class="tips">
                        <span style="color: #ff6b00">注意事项：</span
                        >您选择的员工作为群主所拥有的所有群将会应用该SOP的规则。
                    </div>
                    <!-- <div class="user_list" :class="{ 'user_list-no': !creators.length }"> -->
                    <render-user-tags v-model:tags="creators" :closable="!props.look" />
                    <!-- <el-tag
                            v-for="tag in creators"
                            :key="tag.id"
                            :closable="!props.look"
                            @close="removeDepart(tag.id)">
                            <span class="tag_name">{{ tag.name }}</span>
                        </el-tag> -->
                    <!-- </div> -->
                    <div>
                        目前识别到<span style="color: #ff6b00">{{ num }}</span
                        >个群聊，<span style="color: #ff6b00; cursor: pointer" @click="getGroupNums">重新计算</span>
                    </div>
                </div>
            </el-form-item>
        </el-form>
        <!--选择员工-->
        <ZWDepart ref="DepartRef" :type="1" @submit="getZWDepartData" />
    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import ZWDepart from '@/components/ZWDepart.vue';
import { defaultTime } from '@/utils/utils';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { getSetionId } from '@/utils/utils';
import { getGroupNum } from '@/api/smartMarketing/sop';
import renderUserTags from '@/components/zw/activity/render-tags/render-user-tags.vue';

const props = defineProps(['formData', 'look']);
const propsFormData = ref(props.formData);
const num = ref(0);
const creators = ref([]);
const DepartRef = ref(null);
const formRef = ref<FormInstance>();
const formRule = reactive<FormRules>({
    name: [
        { required: true, message: '规则名称不能为空', trigger: 'blur' },
        { message: '规则名称最多15个字', max: 15 }
    ],
    validity_type: { required: true, message: '规则名称不能为空', trigger: 'change' },
    validity_time: { required: true, message: '规则有效期不能为空', trigger: ['change', 'blur'] },
    groupchat_ids: { required: true, message: '客户群不能为空', trigger: ['change', 'blur'] }
});
const disabledDate = (time: any) => {
    return time.getTime() < Date.now() - 8.64e7;
};
//打开使用成员弹框
const openGroup = () => {
    DepartRef.value.selectedMember = creators.value || [];
    DepartRef.value.departVisble = true;
    DepartRef.value.isInit = true;
    DepartRef.value.title = '选择员工';
};
// 获取当前选中的员工的数据
const getZWDepartData = (val: any) => {
    creators.value = val;
    propsFormData.value.groupchat_ids = getSetionId('id', val);
    getGroupNums();
};
//获取客户群数
const getGroupNums = (val?: any) => {
    if (!props.look || val == 1) {
        getGroupNum({ work_user_ids: propsFormData.value.groupchat_ids }).then((res: any) => {
            num.value = res.num;
        });
    } else {
        return;
    }
};
// 删除
const removeDepart = (key: number) => {
    creators.value = creators.value.filter((item: any) => item.id !== key);
    propsFormData.value.groupchat_ids = getSetionId('id', creators.value);
};
// next
const next = () => {
    return formRef.value.validate((valid: boolean) => {
        if (propsFormData.value.groupchat_ids.length == 0) {
            ElMessage.warning('群主不能为空！');
        }
        if (propsFormData.value.groupchat_ids.length > 10) {
            ElMessage.warning('客户群最多添加10个！');
        }

        return valid;
    });
};
defineExpose({
    next,
    creators,
    getGroupNums
});
</script>

<style lang="scss" scoped>
.step1 {
    .con_title {
        font-size: 16px;
        font-family: PingFangSC-Medium, 'PingFang SC';
        font-weight: 500;
        color: #33302d;
        margin-bottom: 20px;
    }

    .step1_two {
        border-top: 1px solid #eaeaea;
        padding-top: 30px;
    }

    .step_form {
        .mar0_form_item {
            margin-bottom: 0;
        }

        .el-form-item {
            width: 700px;
        }

        .mar10 {
            margin: 0 10px;
        }
    }

    // .user_list {
    //     margin-top: 10px;

    //     :deep(.el-tag) {
    //         width: auto;
    //         height: auto;
    //         padding: 6px;
    //         margin-right: 16px;
    //         background: #fffaf9;
    //         border: 1px solid #ff6b00;
    //         border-radius: 2px;

    //         .el-tag__content {
    //             display: flex;
    //             align-items: center;
    //         }
    //         .el-tag__close {
    //             margin-left: 10px;
    //         }
    //         .tag_img {
    //             width: 24px;
    //             line-height: 24px;
    //             margin-right: 8px;
    //         }
    //     }
    // }
    // .user_list-no {
    //     margin-top: 0px;
    // }
    .tips {
        box-sizing: border-box;
        margin-top: 16px;
        line-height: 20px;
        width: 580px;
        background: #fffaf9;
        border-radius: 4px;
        padding: 16px;
    }
}
</style>
